package com.lmiot.xyclick.Util;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import java.util.Iterator;
import java.util.Stack;

public class ActivityUtil {
    private static Stack<Activity> activityStack;

    public static void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<>();
        }
        activityStack.add(activity);
    }

    public static Activity currentActivity() {
        return activityStack.lastElement();
    }

    public static void finishActivity() {
        activityStack.lastElement();
    }

    public static void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
        }
    }

    public static void finishActivity(Class<?> cls) {
        Iterator<Activity> it = activityStack.iterator();
        while (it.hasNext()) {
            Activity next = it.next();
            if (next.getClass().equals(cls)) {
                finishActivity(next);
            }
        }
    }

    public static void finishAllActivity() {
        int size = activityStack.size();
        for (int i = 0; i < size; i++) {
            if (activityStack.get(i) != null) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    public static void AppExit(Context context) {
        try {
            finishAllActivity();
            ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).restartPackage(context.getPackageName());
            System.exit(0);
        } catch (Exception unused) {
        }
    }

    public static Stack<Activity> getActivityStack() {
        return activityStack;
    }

    public static boolean isExistActivity(Context context, String str, String str2) {
        Intent intent = new Intent();
        intent.setClassName(str, str2);
        if (context.getPackageManager().resolveActivity(intent, 0) == null || intent.resolveActivity(context.getPackageManager()) == null || context.getPackageManager().queryIntentActivities(intent, 0).size() == 0) {
            return false;
        }
        return true;
    }

    public static void skipActivityAndFinishAll(Context context, Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);
        intent.putExtras(bundle);
        intent.setFlags(268468224);
        context.startActivity(intent);
        ((Activity) context).finish();
    }

    public static void skipActivityAndFinishAll(Context context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        intent.setFlags(268468224);
        context.startActivity(intent);
        ((Activity) context).finish();
    }

    public static void skipActivityAndFinish(Context context, Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);
        intent.putExtras(bundle);
        context.startActivity(intent);
        ((Activity) context).finish();
    }

    public static void skipActivityAndFinish(Context context, Class<?> cls) {
        context.startActivity(new Intent(context, cls));
        ((Activity) context).finish();
    }

    public static void skipActivity(Context context, Class<?> cls) {
        context.startActivity(new Intent(context, cls));
    }

    public static void skipActivity(Context context, Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public static void skipActivityForResult(Activity activity, Class<?> cls, int i) {
        activity.startActivityForResult(new Intent(activity, cls), i);
    }

    public static void skipActivityForResult(Activity activity, Class<?> cls, Bundle bundle, int i) {
        Intent intent = new Intent(activity, cls);
        intent.putExtras(bundle);
        activity.startActivityForResult(intent, i);
    }

    public static String getLauncherActivity(Context context, String str) {
        Intent intent = new Intent("android.intent.action.MAIN", (Uri) null);
        intent.addCategory("android.intent.category.LAUNCHER");
        for (ResolveInfo resolveInfo : context.getPackageManager().queryIntentActivities(intent, 0)) {
            if (resolveInfo.activityInfo.packageName.equals(str)) {
                return resolveInfo.activityInfo.name;
            }
        }
        return "no " + str;
    }
}
